package com.demos.springcloud.provider07HystrixFallback.service.impl;

import com.common.tools.RequestTools;
import com.demos.springcloud.provider07HystrixFallback.service.FallbackService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Service
public class FallbackServiceImpl implements FallbackService {

    //规定 5 秒钟以内就不报错，正常运行，超过 5 秒就报错，调用指定的方法
    @Override
    @HystrixCommand(fallbackMethod = "FallbackMethod01",
            commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")})
    public String method01(String id) {
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return Thread.currentThread().getName() + " 01: " + id;
    }

    //入参需一致，否则找不到方法
    public String FallbackMethod01(String id) {
        System.out.println("fallback01");
        return Thread.currentThread().getName() + " ---------fallback01------: ";
    }

    @Override
    @HystrixCommand(fallbackMethod = "FallbackMethod02")
    public String method02(String id) {
        if (System.currentTimeMillis() % 2 == 0) {
            throw new RuntimeException("报错了07");
        }
        return Thread.currentThread().getName() + " 02: " + id;
    }

    public String FallbackMethod02(String id) {
        System.out.println("fallback02");
        return Thread.currentThread().getName() + " ---------fallback02------: " + id;
    }
}
